package utils.newclass
{
	public class Game_input
	{
		// the current state of the mouse
		public var mouse_is_down:Boolean = false;
		public var mouse_click_x:int = 0;
		public var mouse_click_y:int = 0;
		public var mouse_x:int = 0;
		public var mouse_y:int = 0;
		// the current state of the keyboard controls 
		public var pressing:Object =
			{ up:0, down:0, left:0, right:0, fire:0,
				strafe_left:0, strafe_right:0,
				key0:0, key1:0, key2:0, key3:0, key4:0,
				key5:0, key6:0, key7:0, key8:0, key9:0 };
		// if mouselook is on, this is added to the chase camera
		public var camera_angle_x:Number = 0;
		public var camera_angle_y:Number = 0;
		public var camera_angle_z:Number = 0;
		// if this is true, dragging the mouse changes the camera angle
		public var mouse_look_mode:Boolean = true;
		// the game's main stage
		public var stage:Stage;
		// for click events to be sent to the game
		private var _clickfunc:Function = null;
		
		
		public function Game_input(the_stage:Stage,clickfunc:Function = null)
		{
			stage = the_stage;
			// get keypresses and detect the game losing focus
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
			stage.addEventListener(Event.DEACTIVATE, lostFocus);
			stage.addEventListener(Event.ACTIVATE, gainFocus);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouse_down);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouse_up);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouse_move);
			// handle clicks in game
			_clickfunc = clickfunc;
		}
		
		private function mouse_down(e:MouseEvent):void
		{
			trace('mouse_down at '+e.stageX+','+e.stageY);
			mouse_click_x = e.stageX;
			mouse_click_y = e.stageY;
			mouse_is_down = true;
			if (_clickfunc != null) _clickfunc();
		}
		
		private function keyPressed(event:KeyboardEvent):void
		{
			// qwer 81 87 69 82
			// asdf 65 83 68 70
			// left right 37 39
			// up down 38 40
			// 0123456789 = 48 to 57
			// zxcv = 90 88 67 86
			// trace("keyPressed " + event.keyCode);
			if (event.ctrlKey ||
				event.altKey ||
				event.shiftKey)
				pressing.fire = true;
			switch(event.keyCode)
			{
				case 81: // Q
					pressing.strafe_left = true;
					break;
				case 69: // E
					pressing.strafe_right = true;
					break;
				case Keyboard.UP:
				case 87:
					pressing.up = true;
					break;
				case Keyboard.DOWN:
				case 83:
					pressing.down = true;
					break;
				case Keyboard.LEFT:
				case 65:
					pressing.left = true;
					break;
				case Keyboard.RIGHT:
				case 68:
					pressing.right = true;
					break;
				case Keyboard.SPACE:
				case Keyboard.SHIFT:
				case Keyboard.CONTROL:
				case Keyboard.ENTER:
				case 90: // z
				case 88: // x
				case 67: // c
					pressing.fire = true;
					break;
				case 48: pressing.key0 = true; break;
				case 49: pressing.key1 = true; break;
				case 50: pressing.key2 = true; break;
				case 51: pressing.key3 = true; break;
				case 52: pressing.key4 = true; break;
				case 53: pressing.key5 = true; break;
				case 54: pressing.key6 = true; break;
				case 55: pressing.key7 = true; break;
				case 56: pressing.key8 = true; break;
				case 57: pressing.key9 = true; break;
			}
		}
		
		private function gainFocus(event:Event):void
		{
			trace("Game received keyboard focus.");
		}
		
		// if the game loses focus, don't keep keys held down
		private function lostFocus(event:Event):void
		{
			trace("Game lost keyboard focus.");
			pressing.up = false;
			pressing.down = false;
			pressing.left = false;
			pressing.right = false;
			pressing.strafe_left = false;
			pressing.strafe_right = false;
			pressing.fire = false;
			pressing.key0 = false;
			pressing.key1 = false;
			pressing.key2 = false;
			pressing.key3 = false;
			pressing.key4 = false;
			pressing.key5 = false;
			pressing.key6 = false;
			pressing.key7 = false;
			pressing.key8 = false;
			pressing.key9 = false;
		}
		
		private function keyReleased(event:KeyboardEvent):void
		{
			switch(event.keyCode)
			{
				case 81: // Q
					pressing.strafe_left = false;
					break;
				case 69: // E
					pressing.strafe_right = false;
					break;
				case Keyboard.UP:
				case 87:
					pressing.up = false;
					break;
				case Keyboard.DOWN:
				case 83:
					pressing.down = false;
					break;
				case Keyboard.LEFT:
				case 65:
					pressing.left = false;
					break;
				case Keyboard.RIGHT:
				case 68:
					pressing.right = false;
					break;
				case Keyboard.SPACE:
				case Keyboard.SHIFT:
				case Keyboard.CONTROL:
				case Keyboard.ENTER:
				case 90: // z
				case 88: // x
				case 67: // c
					pressing.fire = false;
					break;
				case 48: pressing.key0 = false; break;
				case 49: pressing.key1 = false; break;
				case 50: pressing.key2 = false; break;
				case 51: pressing.key3 = false; break;
				case 52: pressing.key4 = false; break;
				case 53: pressing.key5 = false; break;
				case 54: pressing.key6 = false; break;
				case 55: pressing.key7 = false; break;
				case 56: pressing.key8 = false; break;
				case 57: pressing.key9 = false; break;
			}
		}
		
		/**end class*/
	}
}